package com.ccit.springbootlaw.controller.base;

import com.ccit.springbootlaw.controller.resful.Code;
import com.ccit.springbootlaw.controller.resful.Result;
import com.ccit.springbootlaw.pojo.Comment;
import com.ccit.springbootlaw.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;

@RestController
@RequestMapping("/api/comments")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 根据ID查询评论
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable int id) {
        Comment comment = commentService.findById(id);
        if (comment != null) {
            return new Result(Code.GET_OK, comment, "查询成功");
        } else {
            return new Result(Code.GET_ERR, null, "评论不存在");
        }
    }

    /**
     * 根据用户ID查询评论
     */
    @GetMapping("/user/{userId}")
    public Result findByUserId(@PathVariable int userId) {
        List<Comment> comments = commentService.findByUserId(userId);
        return new Result(Code.GET_OK, comments, "查询成功");
    }

    /**
     * 根据文章ID查询评论
     */
    @GetMapping("/article/{articleId}")
    public Result findByArticleId(@PathVariable int articleId) {
        List<Comment> comments = commentService.findByArticleId(articleId);
        return new Result(Code.GET_OK, comments, "查询成功");
    }

    /**
     * 根据父评论ID查询评论
     */
    @GetMapping("/parent/{parentId}")
    public Result findByParentId(@PathVariable Integer parentId) {
        List<Comment> comments = commentService.findByParentId(parentId);
        return new Result(Code.GET_OK, comments, "查询成功");
    }

    /**
     * 根据创建时间范围查询评论
     */
    @GetMapping("/range")
    public Result findByCreatedAtRange(
            @RequestParam Timestamp startTime,
            @RequestParam Timestamp endTime) {
        List<Comment> comments = commentService.findByCreatedAtRange(startTime, endTime);
        return new Result(Code.GET_OK, comments, "查询成功");
    }

    /**
     * 根据条件查找案件
     *
     * @param comment 留言信息对象，包含查询条件
     * @return 返回查询结果
     */
    @PostMapping("/search")
    public Result findByCriteria(@RequestBody Comment comment) {
        List<Comment> result = commentService.findByConditions(comment);
        if (result != null) {
            return new Result(Code.GET_OK, result, "查询成功");
        } else {
            return new Result(Code.GET_ERR, null, "查询失败，未找到符合条件的留言");
        }
    }

    /**
     * 查询所有评论
     */
    @GetMapping
    public Result findAll() {
        List<Comment> comments = commentService.findAll();
        return new Result(Code.GET_OK, comments, "查询成功");
    }

    /**
     * 插入新的评论
     */
    @PostMapping
    public Result insertComment(@RequestBody Comment comment) {
        int rows = commentService.insertComment(comment);
        if (rows > 0) {
            return new Result(Code.ADD_OK, comment, "添加成功");
        } else {
            return new Result(Code.ADD_ERR, null, "添加失败");
        }
    }

    /**
     * 更新评论
     */
    @PutMapping("/update")
    public Result updateComment(@RequestBody Comment comment) {
        int rows = commentService.updateComment(comment);
        if (rows > 0) {
            return new Result(Code.UPDATE_OK, comment, "更新成功");
        } else {
            return new Result(Code.UPDATE_ERR, null, "更新失败");
        }
    }

    /**
     * 删除评论
     */
    @DeleteMapping("/{id}")
    public Result deleteCommentById(@PathVariable int id) {
        int rows = commentService.deleteCommentById(id);
        if (rows > 0) {
            return new Result(Code.DELETE_OK, null, "删除成功");
        } else {
            return new Result(Code.DELETE_ERR, null, "删除失败");
        }
    }
}